<template>
  <div class="role">
    <page-search
      :searchFormConfig="searchFormConfig"
      @handleResetBtnClick="handleResetBtnClick"
      @handleSearchBtnClick="handleSearchBtnClick"
    ></page-search>
    <!-- 使用了组件,组件自己就会去vuex拿到数据,不用再次传递,只需传递给他url路径(pageName). -->
    <page-content
      pageName="role"
      :tableConfig="contentTableConfig"
      ref="pageContentRef"
      @pageContentEditBtnClick="pageContentEditBtnClick"
      @pageContentCreateBtnClick="pageContentCreateBtnClick"
    ></page-content>
    <page-modal
      :pageModalFormConfig="pageModalFormConfig"
      pageName="role"
      :defaultInfo="defaultInfo"
      ref="pageModalRef"
      :otherInfo="otherInfo"
    >
      <el-tree
        ref="elTreeRef"
        :data="menu"
        show-checkbox
        node-key="id"
        :props="{ children: 'children', label: 'name' }"
        @check="handleCheckChange"
      />
    </page-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, nextTick } from 'vue';
import { useStore } from '@/store';

import { ElTree } from 'element-plus';
import { usePageModal } from '@/hooks/use-page-modal';
import pageContent from '@/components/page-content';
import pageSearch from '@/components/page-search/index';
import pageModal from '@/components/page-modal';
import { contentTableConfig } from './config/content.config';
import { searchFormConfig } from './config/search.config';
import { pageModalFormConfig } from './config/modal.config';
import { usePageSearch } from '@/hooks/use-page-search';
import { mapMenuToLeafKeys } from '@/utils/mapMenus';

export default defineComponent({
  name: 'role',
  components: {
    pageContent,
    pageSearch,
    pageModal
  },
  setup() {
    const store = useStore();
    const menu = computed(() => store.state.menuList);
    const otherInfo = ref({});
    const elTreeRef = ref<InstanceType<typeof ElTree>>();
    const handleCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys;
      const halfCheckedKeys = data2.halfCheckedKeys;
      const menuList = [...checkedKeys, ...halfCheckedKeys]; //两个都是数组
      otherInfo.value = { menuList: menuList }; // otherInfo.menuList
    };

    // usePageSearch
    // item传递到这的顺序是: pageContent点击编辑传递事件携带item, role写一个hook监听事件接收item. hook是一个函数, role使用hook时写createCb和editCb作为参数传给hook, hook执行其逻辑时会执行回调函数,在执行回调函数时又将item传递给会role.
    const editCallBack = (item: any) => {
      const leafKeys = mapMenuToLeafKeys(item.menuList);
      console.log(item);
      console.log(leafKeys);
      // ref这时还没有绑定
      nextTick(() => {
        elTreeRef.value?.setCheckedKeys(leafKeys, false);
      });
    };
    const [pageContentRef, handleResetBtnClick, handleSearchBtnClick] =
      usePageSearch();
    const [
      pageModalRef,
      defaultInfo,
      pageContentCreateBtnClick,
      pageContentEditBtnClick
    ] = usePageModal(null, editCallBack, ['新建角色', '编辑角色']);
    return {
      contentTableConfig,
      searchFormConfig,
      pageContentRef,
      handleResetBtnClick,
      handleSearchBtnClick,
      pageModalFormConfig,
      pageModalRef,
      defaultInfo,
      pageContentCreateBtnClick,
      pageContentEditBtnClick,
      menu,
      otherInfo,
      handleCheckChange,
      elTreeRef
    };
  }
});
</script>

<style scoped lang="less"></style>
